/*******************************************************************************
 * Copyright (c) 2012 March and Conquest.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package mrusanov.towers.stats;

import java.util.Collections;
import java.util.List;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

/**
 * Class which encapsulates players pair and contains information related to
 * game statistics between them. <br>
 * <br>
 * WARNING!<br>
 * Equals method of this class takes into consideration only names of players
 * and ignores other fields
 * 
 * 
 */
public class PlayersPair {
	private final String firstPlayer;
	private final String secondPlayer;
	private int firstWon;
	private int secondWon;

	private List<GameStatistics> statistics = Lists.newArrayList();

	public PlayersPair(String firstPlayer, String secondPlayer) {
		super();
		this.firstPlayer = firstPlayer;
		this.secondPlayer = secondPlayer;
	}

	public int getFirstWon() {
		return firstWon;
	}

	public int getSecondWon() {
		return secondWon;
	}

	public void addResult(GameStatistics stats) {
		statistics.add(stats);

		if (stats.getWinner().equals(firstPlayer)) {
			firstWon++;
		} else {
			secondWon++;
		}
	}

	public ImmutableList<GameStatistics> getGames() {
		Collections.sort(statistics);
		return ImmutableList.copyOf(statistics);
	}

	public String getFirstPlayer() {
		return firstPlayer;
	}

	public String getSecondPlayer() {
		return secondPlayer;
	}

	@Override
	public int hashCode() {
		return firstPlayer.hashCode() + secondPlayer.hashCode();
	}

	@Override
	public String toString() {
		return firstPlayer + " - " + secondPlayer;
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof PlayersPair)) {
			return false;
		}

		PlayersPair pair = (PlayersPair) obj;
		return (pair.getFirstPlayer().equals(getFirstPlayer()) && pair.getSecondPlayer().equals(getSecondPlayer()))
				|| (pair.getFirstPlayer().equals(getSecondPlayer()) && pair.getSecondPlayer().equals(getFirstPlayer()));
	}
}
